package com.example.ylgh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.exception.YyghException;
import com.example.result.Result;
import com.example.result.ResultCodeEnum;
import com.example.utils.MD5;
import com.example.ylgh.mapper.HospitalSetMapper;
import com.example.ylgh.model.hosp.HospitalSet;
import com.example.ylgh.service.HospitalSetService;
import com.example.ylgh.vo.hosp.HospitalSetQueryVo;
import com.example.ylgh.vo.order.SignInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Random;


/**
 * @author hx
 * @create 2022/5/7 10:48
 * description:
 */
@Service
public class HospitalSetServiceImpl extends ServiceImpl<HospitalSetMapper,HospitalSet> implements HospitalSetService {


    @Override
    public Result findPageHospSet(long current, long limit, HospitalSetQueryVo hospitalSetQueryVo) {
        LambdaQueryWrapper<HospitalSet> queryWrapper = new LambdaQueryWrapper<>();
        //判断查询条件是否为空
        String hoscode = hospitalSetQueryVo.getHoscode();
        if (!StringUtils.isEmpty(hoscode)){
            queryWrapper.eq(HospitalSet::getHoscode,hospitalSetQueryVo.getHoscode());
        }
        String hosname = hospitalSetQueryVo.getHosname();
        if (!StringUtils.isEmpty(hosname)){
            queryWrapper.like(HospitalSet::getHosname,hospitalSetQueryVo.getHosname());
        }
        Page<HospitalSet> page = new Page<>(current, limit);
        Page<HospitalSet> hospitalSetPage = page(page, queryWrapper);
        return Result.ok(hospitalSetPage);
    }

    @Override
    public Result saveHospSet(HospitalSet hospitalSet) {
        //设置医院的状态  1表示可以使用   0表示不能使用
        hospitalSet.setStatus(1);
        //设置签名密钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+ random.nextInt(1000)));
        boolean save = save(hospitalSet);
        if (save){
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @Override
    public Result lockHospSet(long id, Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = getById(id);
        //设置医院的状态
        hospitalSet.setStatus(status);
        //更新医院信息
        updateById(hospitalSet);
        return Result.ok();
    }

    @Override
    public String getSignKey(String hoscode) {
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        wrapper.eq("hoscode",hoscode);
        HospitalSet hospitalSet = baseMapper.selectOne(wrapper);
        return hospitalSet.getSignKey();
    }

    @Override
    public SignInfoVo getSignInfoVo(String hoscode) {
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        wrapper.eq("hoscode",hoscode);
        HospitalSet hospitalSet = baseMapper.selectOne(wrapper);
        if(null == hospitalSet) {
            throw new YyghException(ResultCodeEnum.HOSPITAL_OPEN);
        }
        SignInfoVo signInfoVo = new SignInfoVo();
        signInfoVo.setApiUrl(hospitalSet.getApiUrl());
        signInfoVo.setSignKey(hospitalSet.getSignKey());
        return signInfoVo;
    }

}
